package com.bulletelc.apollopre.module.main.center.afterSale;

import com.bulletelc.apollopre.MyApplication;
import com.bulletelc.apollopre.constants.GlobalConfig;
import com.bulletelc.apollopre.constants.NetWorkPath;
import com.bulletelc.apollopre.entity.ServiceOrderBean;
import com.bulletelc.apollopre.entity.ServiceOrderGoodBean;
import com.bulletelc.apollopre.framework.BasePresenter;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by CX on 2018/1/26.
 */

public class ServiceOrderPresenter extends BasePresenter<ServiceOrderContract.View> implements ServiceOrderContract.Presenter {

    @Override
    public void getServiceOrderList() {
        getSOList();
    }

    /**
     * 获取售后订单列表
     */
    private void getSOList() {
        OkGo.<String>get(NetWorkPath.GET_SERVICE_ORDER_LIST)
                .headers(NetWorkPath.HEADER_KEY_AUTH, GlobalConfig.AUTH_HEAD + MyApplication.getToken())
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        parseServiceOrder(response);
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        if (isViewAttached()) {
                            getView().onErrorServiceOrderList(response.getException().toString());
                        }
                    }
                });
    }

    /**
     * 解析售后订单数据
     * @param response
     */
    private void parseServiceOrder(Response<String> response) {
        if (response.isSuccessful()) {
            try {
                JSONObject obj = new JSONObject(response.body().toString());
                JSONObject objMeta = new JSONObject(obj.getString("meta"));
                if (GlobalConfig.HTTP_RESULT_SUCCESS.equals(objMeta.getString("code"))) {
                    JSONArray arraySub = obj.getJSONArray("data");
                    List<ServiceOrderBean> sobList = new ArrayList<>();
                    for (int i=0;i<arraySub.length();i++) {
                        ServiceOrderBean sob = new ServiceOrderBean();
                        sob.setId(arraySub.getJSONObject(i).getString("id"));
                        sob.setCode(arraySub.getJSONObject(i).getString("code"));
                        JSONObject objGood = new JSONObject(arraySub.getJSONObject(i).toString());
                        JSONArray arrayGood = objGood.getJSONArray("product_list");
                        List<ServiceOrderGoodBean> sogList = new ArrayList<>();
                        for (int j=0;j<arrayGood.length();j++) {
                            ServiceOrderGoodBean sog = new ServiceOrderGoodBean();
                            sog.setQuantity(arrayGood.getJSONObject(j).getInt("quantity"));
                            sog.setProduct_sku_title(arrayGood.getJSONObject(j).getString("product_sku_title"));
                            sog.setProduct_name(arrayGood.getJSONObject(j).getString("product_name"));
                            sog.setLogo_url(arrayGood.getJSONObject(j).getString("logo_url"));
                            sog.setReturn_price(arrayGood.getJSONObject(j).getString("return_price"));
                            sog.setOrigin_price(arrayGood.getJSONObject(j).getString("origin_price"));
                            sogList.add(sog);
                        }
                        sob.setServiceOrderGoodBeans(sogList);
                        sobList.add(sob);
                    }
                    if (isViewAttached()) {
                        getView().onSuccessServiceOrderList(sobList);
                    }
                } else {
                    if (isViewAttached()) {
                        getView().onFailServiceOrderList(objMeta.getString("msg"));
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                if (isViewAttached()) {
                    getView().onErrorServiceOrderList("解析错误");
                }
            }

        } else {
            if (isViewAttached()) {
                getView().onFailServiceOrderList(response.body().toString());
            }
        }
    }

    @Override
    public void start() {

    }

    @Override
    public void stop() {

    }
}
